#include "stdafx.h"
#include "SDLSurface.h"

SDLSurface::SDLSurface()
{
}

SDLSurface::~SDLSurface()
{
}

SDL_Surface* SDLSurface::OnLoad(char* File)
{
	SDL_Surface* Surf_Temp = NULL;
	SDL_Surface* Surf_Return = NULL;

	if((Surf_Temp = SDL_LoadBMP(File)) == NULL)
	{
		return NULL;
	}

	/*
	Often the image loaded will be in a different pixel format than the display. 
	Thus, when we draw an image to the display we can incurr performance loss, 
	lose image colors, and the like. But, thanks to SDL, it offers a quick solution around this, 
	SDL_DisplayFormat.
	*/
	//SDL_DisplayFormat returns a new Surface, and doesn't overwrite the original
	Surf_Return = SDL_DisplayFormat(Surf_Temp);
	//Since it creates a new surface, we have to free the old one
	SDL_FreeSurface(Surf_Temp);
	
	return Surf_Return;
}

/*
The first surface is the destination surface, or the surface we are going to draw on. 
The second surface is the source surface, or the surface we going to use to draw on another surface. 
Basically, we are putting Surf_Src on top of Surf_Dest.
The X, Y variables is the position on the Surf_Dest where we are drawing this surface to.
*/
bool SDLSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y)
{
	//Make sure we have valid surfaces
	if(Surf_Dest == NULL || Surf_Src == NULL)
	{
		return false;
	}

	//This is a SDL structure that basically has four members: x, y, w, h. 
	//This, of course, creates the dimensions for a rectangle
	SDL_Rect DestR;

	//We are only worried about where we are drawing to, not the size.
	//So we assign X, Y coordinates to the destination surface.
	DestR.x = X;
	DestR.y = Y;

	SDL_BlitSurface(Surf_Src, NULL, Surf_Dest, &DestR);

	return true;
}

bool SDLSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, 
						int X2, int Y2, int W, int H)
{
	if(Surf_Dest == NULL || Surf_Src == NULL)
	{
		return false;
	}

	SDL_Rect DestR;

	DestR.x = X;
	DestR.y = Y;

	SDL_Rect SrcR;

	SrcR.x = X2;  
	SrcR.y = Y2;  
	SrcR.w = W;    
	SrcR.h = H;

	SDL_BlitSurface(Surf_Src, &SrcR, Surf_Dest, &DestR);

	return true;
}